﻿

app.controller('DocumentController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;


    $scope.ShowRenameForm = function (model) {
        var tempmodel = JSON.parse(JSON.stringify(model))
        $scope.ReNameModel = tempmodel;
    }

    $scope.GetDocumentList = function (Id) {
        $scope.DataLoading = true;
        $scope.DocumentList = [];
        if (Id == null) {
            $scope.CurrentCategoryId = 0;
        }
        else {
            $scope.CurrentCategoryId = Id;
        }
        $http({
            method: 'POST',
            url: '../Document/GetDocumentNavList',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.NavList = ret.Data;
                $http({
                    method: 'POST',
                    url: '../Document/GetListByCategory',
                    data: { 'categoryId': Id }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        common.success($scope, null, ret);
                        $scope.DocumentList = ret.Data;
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                    });
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
        $http({
            method: 'POST',
            url: '../Document/GetCategoryUserRole',
            data: { 'CategoryId': Id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.RoleInfo = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
        
       
   
    }

    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../SystemConfig/GetDepartmentList'
        })
            .success(function (ret) {
                $scope.PageLoading = false;
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $("#" + control);
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitRoleConfig = function (model) {
        $scope.Role.CategoryId = model.Id;
        $scope.BindDepartmentList('select_dep');
        $scope.GetRoleByCategory(model.Id);
        $http({
            method: 'POST',
            url: '../Document/GetCategoryDetail',
            data: { 'CategoryId': model.Id }
        }).success(function (ret) {
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                $scope.Role.IsPublic = ret.Data.IsPublic;
            }
        }).error(function (data, status, headers, config) {
            common.error($scope);
            alertify.alert("请检查网络");
        })

    }

    $scope.SaveUserCategoryRole = function () {
        var result = [];
        var arr = [];
        var list = $("input[name^='item_']");
        for (var i = 0; i < list.length; i++) {
            if (list[i].checked) {
                var userno = list[i].id.substr(list[i].id.lastIndexOf("_") + 1, list[i].id.length - list[i].id.lastIndexOf("_"));
                var v = list[i].value;
                var o = {
                    "userno": userno,
                    "v": v
                };
                arr.push(o);
            }
        }
        for (var i = 0; i < arr.length; i++) {
            var obj = {};
            if (arr[i].v == 1) {
                obj.IsAdmin = true;
                obj.IsManage = true;
                obj.IsView = true;
                obj.CategoryId = $scope.Role.CategoryId;
                obj.UserNo = arr[i].userno;
                result.push(obj);
            }
            else if (arr[i].v == 2) {
                obj.IsAdmin = false;
                obj.IsManage = true;
                obj.IsView = true;
                obj.CategoryId = $scope.Role.CategoryId;
                obj.UserNo = arr[i].userno;
                result.push(obj);
            }
            else if (arr[i].v == 3) {
                obj.IsAdmin = false;
                obj.IsManage = false;
                obj.IsView = true;
                obj.CategoryId = $scope.Role.CategoryId;
                obj.UserNo = arr[i].userno;
                result.push(obj);
            }
            else {
                continue;
            }
        }
        $http({
            method: 'POST',
            url: '../Document/AddRoleConfig',
            data: { 'ConfigList': result, 'IsPublic': $scope.Role.IsPublic, 'CategoryId': $scope.Role.CategoryId}
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    $scope.AlertBox("保存成功！");
                    $scope.GetRoleByCategory($scope.Role.CategoryId);
                    if (result.length == 0) {
                        document.getElementById('btn_close3').click();
                    }
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }
    $scope.GetUserListByDepartment = function (Id) {
        $scope.UserList = [];
        $http({
            method: 'POST',
            url: '../SystemConfig/GetUserListByDepartment',
            data: { 'DepartmentId': Id }
        }).success(function (ret) {
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                $scope.UserList = ret.Data;
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }
    $scope.SearchFile = function (name) {
        if (name == null || name == "") {
            return false;
        }
        $scope.DataLoading = true;
        $scope.ResultList = [];
        $http({
            method: 'POST',
            url: '../Document/SearchFile',
            data: { 'Name': name }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.ResultList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetDepartmentList = function () {
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../OHS/GetDepartmentList',
        })
            .success(function (ret) {
                $scope.DepartmentList = ret.Data;

            })
            .error(function (data, status, headers, config) {
            });
    }

    $scope.GetCategory = function (Id, Type) {
        if (Type == "Folder") {
            $scope.GetDocumentList(Id);
            $scope.CurrentCategoryId = Id;
        }
        else {
            location.href = "../Download/DownloadFile?FileId=" + Id;
        }

    }
    $scope.InitCreateFolder = function (model) {
        model.CategoryName = '';
    }
    $scope.AddDocumentCategory = function (name, id) {
        if (name == null || name == "") {
            alertify.alert("请输入文件夹名称！");
            return false;
        }
        var ispublic = $('#yes').is(':checked');
        $http({
            method: 'POST',
            url: '../Document/AddDocumentCategory',
            data: { 'categorName': name, "ParentId": id, 'IsPublic': ispublic }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("保存成功！");
                    $scope.GetDocumentList(id);
                    $('#docfiles').fileinput({
                        language: 'zh'
                    });
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.UpdateFileName = function (model) {
        if (model.Name == null || model.Name == "") {
            alertify.alert("重命名名称不能为空！");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Document/UpdateFileName',
            data: { 'Id': model.Id, "Type": model.Type, "NewName": model.Name }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    $scope.AlertBox("保存成功！");
                    $scope.GetDocumentList(ret.Data);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.AddRole = function (model) {
        if (model.RoleName == null || model.RoleName == "") {
            alertify.alert("角色名称不能为空！");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Document/AddRole',
            data: { 'model': model }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    $scope.AlertBox("保存成功！");
                    //$scope.GetDocumentList(ret.Data);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetRolePageList = function (model,page) {
        $scope.DataLoading = true;
        $scope.RoleList = [];
        $http({
            method: 'POST',
            url: '../Document/GetRolePageList',
            data: { 'RoleName': model.RoleName },
            param: { 'page': page}
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.RoleList = ret.Data.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.SetRoleId = function (Id) {
        $scope.RoleId = id;
    }

    $scope.GetRoleByCategory = function (Id) {
        $scope.UserRoleList = [];
        $http({
            method: 'POST',
            url: '../Document/GetUserRoleByCategory',
            data: { 'CategoryId': Id}
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.UserRoleList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    //$scope.SaveRoleConfig = function () {
    //    var obj1 = {
    //        RoleId: $scope.RoleId,
    //        Type: '1',
    //        IsManage
    //    };


    //}

    $scope.DeleteFileOrCategory = function (model) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除【" + model.Name + "】？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Document/DeleteCategoryOrFile',
                    data: { 'Id': model.Id, "Type": model.Type }
                })
                    .success(function (ret) {
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetDocumentList(ret.Data);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.DeleteUserRole = function (model) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除【" + model.UserName + "】？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Document/DeleteCategoryConfig',
                    data: { 'Id': model.Id }
                })
                    .success(function (ret) {
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetRoleByCategory(model.CategoryId);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.UploadDocument = function (id) {
        var files = document.querySelector('input[type=file]').files;
        if (files.length > 10) {
            alertify.alert("上传文件数量不能大于10个！");
        }
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../Document/AddDocument',
            data: { 'ParentId': id },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                $scope.GetDocumentList(id);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetOrgLevel = function () {
        $scope.OrgLevelList = [];
        $http({
            method: 'POST',
            url: '../Law/GetOrgLevel'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.OrgLevelList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.BindOrgList = function (control, defaultvalue) {
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Law/GetOrgList',
        })
            .success(function (ret) {
                $scope.HarmList = ret.Data;
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null && defaultvalue.length > 0) {
                        if (defaultvalue.includes(n.Id))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                    }

                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
            });
    }

    $scope.EditDocument = function (model, status, page) {
        if (model.DocumentNo == null || model.DocumentNo == "") {
            alertify.alert("请输入文件编号！");
            return false;
        }
        if (model.DocumentName == null || model.DocumentName == "") {
            alertify.alert("请输入文件名称！");
            return false;
        }
        $scope.DataLoading = true;
        var Orglist = $("#select_org").val();
        var files = document.querySelector('input[type=file]').files;
        model.Status = status;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../Law/EditLawForm',
            data: { 'model': model, "OrgList": Orglist },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("发布成功！");
                }
                setTimeout(function () {
                    window.location.href = "LawDocumentPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }  
    


});




app.filter('HideCardNo', function () { //可以注入依赖
    return function (text) {
        if (text != null && text != "")
            return text.replace(/^(.{3})(?:\d+)(.{4})$/, "$1******$2");
    }
});
